package com.ruoyi.tuanzhang.service.impl;

import cn.hutool.core.util.ArrayUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.listener.PageReadListener;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.ruoyi.tuanzhang.domain.*;
import com.ruoyi.tuanzhang.domain.vo.*;

import com.ruoyi.tuanzhang.domain.excelDto.ImportDarenOwnerExcelDto;
import com.ruoyi.tuanzhang.domain.excelDto.ImportDarenWechatExcelDto;
import com.ruoyi.tuanzhang.domain.excelDto.SelfAddDarenExcelDto;
import com.ruoyi.tuanzhang.mapper.*;
import com.ruoyi.tuanzhang.mapper.impl.DarenBaseMapperImpl;
import com.ruoyi.tuanzhang.service.IParseRequestService;
import com.ruoyi.tuanzhang.utils.UnzipUtils;
import org.apache.commons.compress.utils.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

/**
 * 解析抖音电商里面的请求数据
 *
 */
@Service
public class ParseRequestServiceImpl implements IParseRequestService {


    private static final Logger logger = LoggerFactory.getLogger(ParseRequestServiceImpl.class);

    @Resource private DarenBaseMapper darenBaseMapper;
    @Resource private DarenHomeMapper darenHomeMapper;
    @Resource private DarenHomeFansDataMapper darenHomeFansDataMapper;
    @Resource private DarenHomeFansLikeMapper darenHomeFansLikeMapper;
    @Resource private DarenHomeScoreDataMapper darenHomeScoreDataMapper;
    @Resource private DarenHomeSellDataMapper darenHomeSellDataMapper;
    @Resource private DarenHomeVideoDataMapper darenHomeVideoDataMapper;
    @Resource private DarenHomeVideoHistoryMapper darenHomeVideoHistoryMapper;
    @Resource private DarenLiveDetailsDataMapper darenLiveDetailsDataMapper;
    @Resource private DarenVideoDetailsDataMapper darenVideoDetailsDataMapper;
    @Resource private DarenHomeTagMapper darenHomeTagMapper;

    @Resource private ProductDataMapper productDataMapper;

    private static String getProfile = "GET https://buyin.jinritemai.com/square_pc_api/homePage/author/profile\\?";
    private static String getFansPortrait = "GET https://buyin.jinritemai.com/square_pc_api/homePage/overview/fans_portrait\\?";
    private static String getAuthorReputationDetail = "GET https://buyin.jinritemai.com/api/authorStatData/authorReputationDetail\\?";
    private static String getGoodsPortrait = "GET https://buyin.jinritemai.com/square_pc_api/homePage/overview/goods_portrait\\?";
    private static String getCore = "GET https://buyin.jinritemai.com/square_pc_api/homePage/overview/core\\?";
    private static String getHistoryContent = "GET https://buyin.jinritemai.com/square_pc_api/homePage/overview/history_content\\?";
    private static String getButton = "GET https://buyin.jinritemai.com/api/contact/button\\?";
    private static String getLiveDetails = "GET https://buyin.jinritemai.com/api/authorStatData/authorLiveDetailList\\?";
    private static String getVideoDetails = "GET https://buyin.jinritemai.com/api/authorStatData/authorVideoDetailList\\?";


    private static String profile = "square_pc_api/homePage/author/profile";
    private static String fansPortrait = "square_pc_api/homePage/overview/fans_portrait";
    private static String authorReputationDetail = "api/authorStatData/authorReputationDetail";
    private static String goodsPortrait = "square_pc_api/homePage/overview/goods_portrait";
    private static String core = "square_pc_api/homePage/overview/core";
    private static String historyContent = "square_pc_api/homePage/overview/history_content";
    private static String button = "buyin.jinritemai.com/api/contact/button";
    private static String liveDetails = "buyin.jinritemai.com/api/authorStatData/authorLiveDetailList";
    private static String videoDetails = "buyin.jinritemai.com/api/authorStatData/authorVideoDetailList";

    private static String willPassDarenMain = "酒类虚拟充值美妆";

    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    /**     解析达人列表数据      */
    @Override
    public String parseDarenList(String tarPaths) {
        Set<String> fileSet = parseSaz(tarPaths);
        if (Objects.nonNull(fileSet) && !fileSet.isEmpty()) {
            return getDarenList(fileSet);
        }
        return tarPaths;
    }

    @Override
    public String parseProductList(String tarPaths) {
        Set<String> fileSet = parseSaz(tarPaths);
        if (Objects.nonNull(fileSet) && !fileSet.isEmpty()) {
            return getProductList(fileSet);
        }
        return tarPaths;
    }


    /**     解析达人主页数据      */
    @Override
    public String parseDarenHome(String tarPaths) {
        Set<String> fileSet = parseSaz(tarPaths);
        if (Objects.nonNull(fileSet) && !fileSet.isEmpty()) {
            return getDarenDetailsData(fileSet);
        }
        return tarPaths;
    }

    /***/
    @Override
    public String importDarenOwner(String filePath) {
//        AtomicInteger i = new AtomicInteger();
        Map<String, ImportDarenOwnerExcelDto> map = new HashMap<>();
        EasyExcel.read(filePath, ImportDarenOwnerExcelDto.class, new PageReadListener<ImportDarenOwnerExcelDto>(dataList -> {
            for (ImportDarenOwnerExcelDto dto : dataList) {
                if (Objects.isNull(map.get(dto.getAwemeId()))) {
                    map.put(dto.getAwemeId(), dto);
                } else {
                    if (dto.getOwnerTime().before(map.get(dto.getAwemeId()).getOwnerTime())) {
                        map.put(dto.getAwemeId(), dto);
                    }
                }
            }
        })).sheet().doRead();
        List<ImportDarenOwnerExcelDto> list = Lists.newArrayList();
        map.forEach((k, v) -> {
            list.add(v);
        });
        if (Objects.nonNull(list) && !list.isEmpty()) {
            for (ImportDarenOwnerExcelDto excelDto : list) {
                DarenBaseDo darenBaseDo = new DarenBaseDo();
                BeanUtils.copyProperties(excelDto, darenBaseDo);
                DarenBaseDo baseDo = darenBaseMapper.selectById(excelDto.getAwemeId());
                // 如果是新达人或者归属时间比当前还早就更新或者插入
                if (Objects.isNull(baseDo) || Objects.isNull(baseDo.getOwnerTime()) ||
                        (Objects.nonNull(baseDo) && baseDo.getOwnerTime().after(darenBaseDo.getOwnerTime()))) {
                    darenBaseMapper.insertOrUpdate(darenBaseDo);
                }
            }
        }

        return "写入完成";
    }
    /**
     *
     * 查询到微信号后更新表格内容，后续查找微信号信息不再从这里面获取
     * */
    @Override
    public String importDarenWechat(String filePath) {
        EasyExcel.read(filePath, ImportDarenWechatExcelDto.class, new PageReadListener<ImportDarenWechatExcelDto>(dataList -> {
            for (ImportDarenWechatExcelDto dto : dataList) {
                LambdaQueryWrapper<DarenBaseDo> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(DarenBaseDo::getAwemeId, dto.getAwemeId());
                DarenBaseDo darenBaseDo = darenBaseMapper.selectOne(wrapper);
                if (Objects.nonNull(darenBaseDo)){
                    darenBaseDo.setWechat(dto.getWechat());
                    darenBaseMapper.insertOrUpdate(darenBaseDo);
                }
            }
        })).sheet().doRead();

        return "写入完成";
    }

    /** 获取商品列表中的信息, 把数据保存到数据库 */
    private String getProductList(Set<String> fileSet) {
//        List<DarenListDto> list = new ArrayList<>();
        for (String filePath : fileSet) {
            File fileFolder = new File(filePath);
            // 文件夹
            File[] files = fileFolder.listFiles();
            // 获取所有响应信息，进行解析(多层for，不追求速度，就是任性)
            for (File file : files) {
                boolean contains = file.getName().contains("_s.txt");
                if (contains) {
                    Path path = Paths.get(file.getPath());
                    List<String> allLine = null;
                    String lineTxt = null;

                    try {
                        allLine = Files.readAllLines(path);
                        lineTxt = allLine.get(allLine.size() -1);
                        ProductDataDto productDataDto = JSON.parseObject(lineTxt, ProductDataDto.class);
                        if(Objects.nonNull(productDataDto) && Objects.nonNull(productDataDto.getData()) &&
                                 !productDataDto.getData().isEmpty()) {
                            for (ProductDataDto.DataDTO dto : productDataDto.getData()) {
                                ProductDataDo productData = new ProductDataDo();
                                BeanUtils.copyProperties(dto, productData);
                                if (Objects.nonNull(dto.getCommissionEffectStartTime())) {
                                    productData.setCommissionEffectStartTime(sdf.parse(dto.getCommissionEffectStartTime()));
                                }
                                if (Objects.nonNull(dto.getCommissionEffectEndTime())) {
                                    productData.setCommissionEffectEndTime(sdf.parse(dto.getCommissionEffectEndTime()));
                                }
                                productData.setIsOffTheShelf(dto.getIsOffTheShelf() ? 0 : 1);
                                StringBuilder sb = new StringBuilder();
                                sb.append(productData.getProductName());
                                sb.append("\n【售价】");
                                if (Objects.nonNull(productData.getProductPrice())) {
                                    sb.append(BigDecimal.valueOf(productData.getProductPrice()).divide(BigDecimal.valueOf(100)));
                                }

                                sb.append("\n【佣金】");
                                if (Objects.nonNull(productData.getCommissionRate())) {
                                    sb.append(BigDecimal.valueOf(productData.getCommissionRate()));
                                }

                                sb.append("%\n【链接】");
                                sb.append(productData.getPromotionUrl());
                                productData.setWillSendData(sb.toString());
                                productDataMapper.insertOrUpdate(productData);
                            }
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }
            }
//            logger.info(JSONArray.toJSONString(exportDtoList));
        }
        return "解析结束";
    }


    /** 获取达人列表中的信息, 把数据保存到数据库 */
    private String getDarenList(Set<String> fileSet) {
//        List<DarenListDto> list = new ArrayList<>();
        for (String filePath : fileSet) {
            File fileFolder = new File(filePath);
            // 文件夹
            File[] files = fileFolder.listFiles();
            // 获取所有响应信息，进行解析(多层for，不追求速度，就是任性)
            List<DarenBaseDo> exportDtoList = new ArrayList<>();
            for (File file : files) {
                boolean contains = file.getName().contains("_s.txt");
                if (contains) {
                    Path path = Paths.get(file.getPath());
                    List<String> allLine = null;
                    String lineTxt = null;

                    try {
                        allLine = Files.readAllLines(path);
                        lineTxt = allLine.get(allLine.size() -1);
                            DarenListDto darenListDto = JSON.parseObject(lineTxt, DarenListDto.class);
                        if(Objects.nonNull(darenListDto) && Objects.nonNull(darenListDto.getData()) &&
                            Objects.nonNull(darenListDto.getData().getList()) && !darenListDto.getData().getList().isEmpty()) {
                            for (DarenListDto.DataDTO.ListDTO dto : darenListDto.getData().getList()) {
                                DarenBaseDo darenBase = new DarenBaseDo();
                                BeanUtils.copyProperties(dto.getAuthorBase(), darenBase);
                                DarenBaseDo darenBaseDo = darenBaseMapper.selectById(darenBase.getAwemeId());
                                if (Objects.isNull(darenBaseDo)) {
                                    darenBase.setNickname(darenBase.getNickname());
                                    darenBase.setCreateTime(new Date());
                                    darenBase.setUpdateTime(new Date());
                                } else {
                                    darenBase.setUpdateTime(new Date());
                                }

                                BeanUtils.copyProperties(dto.getAuthorTag(), darenBase);
                                BeanUtils.copyProperties(dto.getAuthorLive(), darenBase);
                                BeanUtils.copyProperties(dto.getAuthorVideo(), darenBase);
                                BeanUtils.copyProperties(dto.getAuthorSale(), darenBase);


                                // 如果只有一个种类，且种类是 滋补保健 美妆 服饰内衣 图书教育 母婴宠物 这几个种类中的其中一种，就不继续
                                // 虚拟充值、酒类
                                if (Objects.nonNull(dto.getAuthorTag())
                                        && Objects.nonNull(dto.getAuthorTag().getMainCate())
                                        && !dto.getAuthorTag().getMainCate().isEmpty()) {
//                                    if (dto.getAuthorTag().getMainCate().size() == 1) {
                                        if (willPassDarenMain.contains(dto.getAuthorTag().getMainCate().get(0))) {
                                            continue;
                                        }
//                                    }
                                    darenBase.setMainCate(JSONArray.toJSONString(dto.getAuthorTag().getMainCate()));
                                }

                                // //（千次观看成交量应该超过100 || 观看次数 > 5万） &&
                                //
                                // 1、 发布  视频数量 + 直播数量  应该多于10  2、近30天业绩大于25000
                                // 粉丝数量大于1万
                                if ( Objects.nonNull(darenBase.getFansNum())
                                        && darenBase.getFansNum() > 10000
                                        && Objects.nonNull(darenBase.getSaleD30Low()) &&
                                        darenBase.getSaleD30Low() >= 25000 &&
                                        darenBase.getAllVideoNum30d() +
                                                (Objects.nonNull(darenBase.getAllLiveNum30d()) ? 0 : darenBase.getAllLiveNum30d()) > 10) {
                                    darenBaseMapper.insertOrUpdate(darenBase);
                                    exportDtoList.add(darenBase);
                                }
                            }

                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }
            }
//            logger.info(JSONArray.toJSONString(exportDtoList));
        }
        return "解析结束";
    }


    /** 获取达人主页中的所有信息, 把数据保存到数据库 */
    private String getDarenDetailsData(Set<String> fileSet) {
        for (String filePath : fileSet) {
            File fileFolder = new File(filePath);
            // 文件夹
            File[] files = fileFolder.listFiles();
            // 获取到压缩包中的文件
            List<SellUpdateData> list = new ArrayList<>();
            for (File file : files) {
                boolean requestFile = file.getName().contains("_c.txt");
                if (requestFile) {
                    Path path = Paths.get(file.getPath());
                    // 请求文件第一行信息
                    String fristLine = null;
                    try {
                        IsRequest isRequest = new IsRequest();
                        fristLine = Files.lines(path).findFirst().orElse(null);
                        // 根据请求判断是什么响应数据  以下都是这个域名 GET https://buyin.jinritemai.com
                        isRequest.setDarenHome(fristLine.contains(profile));
                        isRequest.setDarenHomeFansData(fristLine.contains(fansPortrait));
                        isRequest.setDarenHomeScoreData(fristLine.contains(authorReputationDetail));
                        isRequest.setDarenHomeSellData(fristLine.contains(goodsPortrait));
                        isRequest.setDarenHomeVideoData(fristLine.contains(core));
                        isRequest.setDarenHomeVideoHistory(fristLine.contains(historyContent));
                        // 达人是否有联系方式
                        isRequest.setDarenHasContact(fristLine.contains(button));
                        isRequest.setLiveDetails(fristLine.contains(liveDetails));
                        isRequest.setVideoDetails(fristLine.contains(videoDetails));

                        // 对应的响应文件
                        String reposeFile = file.getPath().replace("_c.txt", "_s.txt");
                        if (isRequest.getDarenHome()) {
                            String uid = getUid(fristLine, getProfile);
//                            logger.info("homePage/author/profile 的用户id---------{}", uid);
                            String resposeData = parseData(reposeFile);
                            DarenHomeDto darenHomeDto = JSON.parseObject(resposeData, DarenHomeDto.class);
                            DarenHomeDo darenHomeDo = new DarenHomeDo();
                            darenHomeDo.setUid(uid);
                            BeanUtils.copyProperties(darenHomeDto.getData(), darenHomeDo);
                            darenHomeDo.setDarkHorses(JSONArray.toJSONString(darenHomeDto.getData().getDarkHorses()));
                            darenHomeDo.setProductMainType(JSONArray.toJSONString(darenHomeDto.getData().getProductMainTypeArray()));
//                            List<DarenTagDo> list = new ArrayList<>();
                            if (Objects.nonNull(darenHomeDto.getData().getTags()) &&
                                    !darenHomeDto.getData().getTags().isEmpty()) {
                                QueryWrapper<DarenHomeTagDo> wrapper = new QueryWrapper<>();
                                wrapper.eq("uid", uid);
                                // 先删掉标签表
                                darenHomeTagMapper.delete(wrapper);
                                for (DarenHomeDto.DataDTO.TagsDTO tag : darenHomeDto.getData().getTags()) {
                                    DarenHomeTagDo darenTagDo = new DarenHomeTagDo();
                                    darenTagDo.setUid(uid);
                                    BeanUtils.copyProperties(tag, darenTagDo);

                                    darenHomeTagMapper.insertOrUpdate(darenTagDo);
                                }
                            }
                            darenHomeMapper.insertOrUpdate(darenHomeDo);
                            QueryWrapper<DarenBaseDo> wrapper = new QueryWrapper<>();
                            wrapper.eq("aweme_id", darenHomeDo.getAccountDouyin());
                            DarenBaseDo darenBaseDo = darenBaseMapper.selectOne(wrapper);
                            darenBaseDo.setUnionUid(uid);
                            darenBaseMapper.updateById(darenBaseDo);
//                            logger.info("DarenHomeDo--------{}", JSONObject.toJSONString(darenHomeDo));
//                            logger.info("List<DarenTagDo>--------{}", JSONArray.toJSONString(list));
                        } else if (isRequest.getDarenHomeFansData()) {
                            String uid = getUid(fristLine, getFansPortrait);
//                            logger.info("homePage/overview/fans_portrait 的用户id---------{}", uid);
                            String resposeData = parseData(reposeFile);
                            DarenHomeFansDataDto darenHomeFansDataDto = JSON.parseObject(resposeData, DarenHomeFansDataDto.class);
                            DarenHomeFansDataDo darenHomeFansDataDo = new DarenHomeFansDataDo();

                            darenHomeFansDataDo.setUid(uid);
                            if (Objects.nonNull(darenHomeFansDataDto.getData().getGender())) {
                                darenHomeFansDataDo.setGenderDesc(darenHomeFansDataDto.getData().getGender().getDesc());
                                darenHomeFansDataDo.setGenderRatio(darenHomeFansDataDto.getData().getGender().getRatio());
                            }

                            if (Objects.nonNull(darenHomeFansDataDto.getData().getAge())) {
                                darenHomeFansDataDo.setAgeDesc(darenHomeFansDataDto.getData().getAge().getDesc());
                                darenHomeFansDataDo.setAgeRatio(darenHomeFansDataDto.getData().getAge().getRatio());
                            }

                            if (Objects.nonNull(darenHomeFansDataDto.getData().getCity())) {
                                darenHomeFansDataDo.setCityDesc(darenHomeFansDataDto.getData().getCity().getDesc());
                                darenHomeFansDataDo.setCityRatio(darenHomeFansDataDto.getData().getCity().getRatio());
                            }

                            if (Objects.nonNull(darenHomeFansDataDto.getData().getConsumptionLevel())) {
                                darenHomeFansDataDo.setConsumptionLevelDesc(darenHomeFansDataDto.getData().getConsumptionLevel().getDesc());
                                darenHomeFansDataDo.setConsumptionLevelDetails(JSONObject.toJSONString(darenHomeFansDataDto.getData().getConsumptionLevel().getDetails()));
                            }
                            if (Objects.nonNull(darenHomeFansDataDto.getData().getBuyingPreference())) {
                                // 如果详情不是为空，且有数据
                                if (Objects.nonNull(darenHomeFansDataDto.getData().getBuyingPreference().getDetails())
                                        && !darenHomeFansDataDto.getData().getBuyingPreference().getDetails().isEmpty()) {
                                    QueryWrapper<DarenHomeFansLikeDo> wrapper = new QueryWrapper<>();
                                    wrapper.eq("uid", uid);
                                    // 先删掉标签表
                                    darenHomeFansLikeMapper.delete(wrapper);
                                    for (DarenHomeFansDataDto.DataDTO.BuyingPreferenceDTO.DetailsDTO detail : darenHomeFansDataDto.getData().getBuyingPreference().getDetails()) {
                                        DarenHomeFansLikeDo darenHomeFansLikeDo = new DarenHomeFansLikeDo();
                                        darenHomeFansLikeDo.setUid(uid);
                                        darenHomeFansLikeDo.setDetailsName(detail.getName());
                                        darenHomeFansLikeDo.setDetailsRatio(detail.getRatio());

                                        darenHomeFansLikeMapper.insertOrUpdate(darenHomeFansLikeDo);
                                    }
                                    darenHomeFansDataDo.setBuyingPreferenceDesc(JSONObject.toJSONString(darenHomeFansDataDto.getData().getBuyingPreference().getDesc()));
                                }
                            }
                            darenHomeFansDataMapper.insertOrUpdate(darenHomeFansDataDo);
//                            logger.info("DarenHomeFansDataDo---------{}", JSONObject.toJSONString(darenHomeFansDataDo));
                        } else if (isRequest.getDarenHomeScoreData()) {
                            String uid = getUid(fristLine, getAuthorReputationDetail);
//                            logger.info("authorStatData/authorReputationDetail 的用户id---------{}", uid);
                            String resposeData = parseData(reposeFile);
                            DarenHomeScoreDataDto darenHomeScoreDataDto = JSON.parseObject(resposeData, DarenHomeScoreDataDto.class);
                            DarenHomeScoreDataDto.DataDTO dto = darenHomeScoreDataDto.getData();
                            DarenHomeScoreDataDo entity = new DarenHomeScoreDataDo();
                            entity.setUid(uid);
                            entity.setGoodsScore(dto.getGoodsScore().getScore());
                            entity.setGoodsScoreRating(dto.getGoodsScore().getRating());
                            entity.setLogisticsScore(dto.getLogisticsScore().getScore());
                            entity.setLogisticsScoreRating(dto.getLogisticsScore().getRating());
                            entity.setServiceScoreRating(dto.getServiceScore().getRating());
                            entity.setServiceScoreScore(dto.getServiceScore().getScore());
                            entity.setPercentage(dto.getPercentage());
                            darenHomeScoreDataMapper.insertOrUpdate(entity);
//                            logger.info("DarenHomeScoreDataDo-------{}", JSONObject.toJSONString(entity));
                        } else if (isRequest.getDarenHomeSellData()) {
                            String uid = getUid(fristLine, getGoodsPortrait);
//                            logger.info("homePage/overview/goods_portrait 的用户id---------{}", uid);

                            String resposeData = parseData(reposeFile);
                            DarenHomeSellDataDto darenHomeSellDataDto = JSON.parseObject(resposeData, DarenHomeSellDataDto.class);
                            DarenHomeSellDataDto.DataDTO dto = darenHomeSellDataDto.getData();
                            SellUpdateData sellUpdateData = new SellUpdateData();
                            sellUpdateData.setUid(uid);
                            sellUpdateData.setCategoryTopDesc(dto.getCategoryTop().getDesc());
                            sellUpdateData.setBrandTopDecs(dto.getBrandTop().getDesc());
                            list.add(sellUpdateData);
                            if (Objects.nonNull(dto.getCategoryTop().getDetails())) {
                                QueryWrapper<DarenHomeSellDataDo> wrapper = new QueryWrapper<>();

                                wrapper.eq("uid", uid);
                                // 先删掉标签表
                                darenHomeSellDataMapper.delete(wrapper);
                                for (int i = 0; i < dto.getCategoryTop().getDetails().size(); i++) {
                                    DarenHomeSellDataDo darenHomeSellDataDo = new DarenHomeSellDataDo();
                                    darenHomeSellDataDo.setUid(uid);
                                    darenHomeSellDataDo.setCategoryTopDetailsName(dto.getCategoryTop().getDetails().get(i).getName());
                                    darenHomeSellDataDo.setCategoryTopDetailsRatio(dto.getCategoryTop().getDetails().get(i).getRatio());
                                    darenHomeSellDataDo.setBrandTopDetailsName(dto.getBrandTop().getDetails().get(i).getName());
                                    darenHomeSellDataDo.setBrandTopDetailsRatio(dto.getBrandTop().getDetails().get(i).getRatio());

                                    darenHomeSellDataMapper.insertOrUpdate(darenHomeSellDataDo);
                                }
                            }

                        } else if (isRequest.getDarenHomeVideoData()) {
                            String uid = getUid(fristLine, getCore);
//                            logger.info("homePage/overview/core 的用户id---------{}", uid);
                            String resposeData = parseData(reposeFile);
                            DarenHomeVideoDataDto darenHomeVideoDataDto = JSON.parseObject(resposeData, DarenHomeVideoDataDto.class);
                            DarenHomeVideoDataDto.DataDTO dto = darenHomeVideoDataDto.getData();
                            DarenHomeVideoDataDo videoDataDo = new DarenHomeVideoDataDo();
                            videoDataDo.setUid(uid);
                            videoDataDo.setMatchingScore(dto.getScores().getMatchingScore().getScore());
                            videoDataDo.setFansScore(dto.getScores().getFansScore().getScore());
                            videoDataDo.setProductScore(dto.getScores().getProductScore().getScore());
                            videoDataDo.setHistoryCooperationScore(dto.getScores().getHistoryCooperationScore().getScore());
                            videoDataDo.setPromotionTotalCount(dto.getSellData().getPromotionTotalCount());
                            for (DarenHomeVideoDataDto.DataDTO.SellDataDTO.DetailsDTO detail : dto.getSellData().getDetails()) {
                                if (StringUtils.equals(detail.getName(), "视频")) {
                                    videoDataDo.setSellDataVideoPromotionCount(detail.getPromotionCount());
                                    videoDataDo.setSellDataVideoTag(detail.getTag());
                                }
                            }
                            darenHomeVideoDataMapper.insertOrUpdate(videoDataDo);
//                            logger.info("videoDataDo--------{}", JSONObject.toJSONString(videoDataDo));
                        } else if (isRequest.getDarenHomeVideoHistory()) {
                            String uid = getUid(fristLine, getHistoryContent);
//                            logger.info("/overview/history_content 的用户id---------{}", uid);
                            String resposeData = parseData(reposeFile);
                            DarenHomeVideoHistoryDto darenHomeVideoHistoryDto = JSON.parseObject(resposeData, DarenHomeVideoHistoryDto.class);
                            DarenHomeVideoHistoryDto.DataDTO dto = darenHomeVideoHistoryDto.getData();
                            List<DarenHomeVideoHistoryDo> doList = new ArrayList<>();
                            QueryWrapper<DarenHomeVideoHistoryDo> wrapper = new QueryWrapper<>();
                            wrapper.eq("uid", uid);
                            // 先删掉标签表
                            darenHomeVideoHistoryMapper.delete(wrapper);
                            for (DarenHomeVideoHistoryDto.DataDTO.ContentListDTO contentListDTO : dto.getContentList()) {
                                DarenHomeVideoHistoryDo videoHistoryDo = new DarenHomeVideoHistoryDo();
                                videoHistoryDo.setUid(uid);
                                BeanUtils.copyProperties(contentListDTO, videoHistoryDo);
                                doList.add(videoHistoryDo);

                                darenHomeVideoHistoryMapper.insertOrUpdate(videoHistoryDo);
                            }
//                            logger.info("videoHistoryDo--------{}", JSONObject.toJSONString(doList));
                        } else if (isRequest.getDarenHasContact()) {

                            String uid = getOriginUid(fristLine, getButton);
//                            logger.info("/overview/history_content 的用户id---------{}", uid);
                            String resposeData = parseData(reposeFile);
                            DarenHasContactDto darenHasContactDto = JSON.parseObject(resposeData, DarenHasContactDto.class);
                            DarenHasContactDto.DataDTO dto = darenHasContactDto.getData();
                            // 根据关联id找用户基本信息
                            DarenBaseDo darenBaseDo = DarenBaseMapperImpl.getDarenBaseByUnionUid(uid);
                            if (Objects.nonNull(darenBaseDo)) {

                                // 1是 2否
                                darenBaseDo.setShowLark(dto.getShowLark() ? 1 : 2);
                                darenBaseDo.setShowPhone(dto.getShowPhone() ? 1 : 2);
                                darenBaseDo.setShowWechat(dto.getShowWechat() ? 1 : 2);
                                darenBaseMapper.insertOrUpdate(darenBaseDo);
                            }
                        } else if (isRequest.getLiveDetails()) {

                            String uid = getUid(fristLine, getLiveDetails);
//                            logger.info("/overview/history_content 的用户id---------{}", uid);
                            String resposeData = parseData(reposeFile);
                            DarenLiveDetailsDataDto darenLiveDetailsDataDto = JSON.parseObject(resposeData, DarenLiveDetailsDataDto.class);
                            DarenLiveDetailsDataDto.DataDTO dto = darenLiveDetailsDataDto.getData();
                            // 根据关联id找用户基本信息
                            DarenBaseDo darenBaseDo = DarenBaseMapperImpl.getDarenBaseByUnionUid(uid);
                            List<DarenLiveDetailsDataDo> doList = new ArrayList<>();
                            QueryWrapper<DarenLiveDetailsDataDo> wrapper = new QueryWrapper<>();
                            wrapper.eq("uid", uid);
                            // 先删掉直播数据表
                            darenLiveDetailsDataMapper.delete(wrapper);

                            if (Objects.nonNull(darenBaseDo)) {
                                if (Objects.nonNull(dto.getTotal()) && dto.getTotal() > 0) {
                                    for (LiveDetailsDataDto liveDetailsDataDto : dto.getList()) {
                                        DarenLiveDetailsDataDo darenLiveDetailsDataDo = new DarenLiveDetailsDataDo();
                                        BeanUtils.copyProperties(liveDetailsDataDto, darenLiveDetailsDataDo);
                                        darenLiveDetailsDataDo.setUid(uid);
                                        doList.add(darenLiveDetailsDataDo);
                                        darenLiveDetailsDataMapper.insertOrUpdate(darenLiveDetailsDataDo);
                                    }
                                }
                            }
                        } else if (isRequest.getVideoDetails()) {

                            String uid = getUid(fristLine, getVideoDetails);
//                            logger.info("/overview/history_content 的用户id---------{}", uid);
                            String resposeData = parseData(reposeFile);
                            DarenVideoDetailsDataDto darenVideoDetailsDataDto = JSON.parseObject(resposeData, DarenVideoDetailsDataDto.class);
                            DarenVideoDetailsDataDto.DataDTO dto = darenVideoDetailsDataDto.getData();
                            // 根据关联id找用户基本信息
                            DarenBaseDo darenBaseDo = DarenBaseMapperImpl.getDarenBaseByUnionUid(uid);
                            List<DarenVideoDetailsDataDo> doList = new ArrayList<>();
                            QueryWrapper<DarenVideoDetailsDataDo> wrapper = new QueryWrapper<>();
                            wrapper.eq("uid", uid);
                            // 先删掉视频数据表
                            darenVideoDetailsDataMapper.delete(wrapper);

                            if (Objects.nonNull(darenBaseDo)) {
                                if (Objects.nonNull(dto.getTotal()) && dto.getTotal() > 0) {
                                    for (VideoDetailsDataDto videoDetailsDataDto : dto.getList()) {
                                        DarenVideoDetailsDataDo darenVideoDetailsDataDo = new DarenVideoDetailsDataDo();
                                        BeanUtils.copyProperties(videoDetailsDataDto, darenVideoDetailsDataDo);
                                        darenVideoDetailsDataDo.setUid(uid);
                                        doList.add(darenVideoDetailsDataDo);
                                        darenVideoDetailsDataMapper.insertOrUpdate(darenVideoDetailsDataDo);
                                    }
                                }
                            }
                        }

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

            }
            if (ArrayUtil.isNotEmpty(list)) {
                for (SellUpdateData sellUpdateData : list) {
                    DarenBaseDo darenBaseDo = DarenBaseMapperImpl.getDarenBaseByUnionUid(sellUpdateData.getUid());
                    if (Objects.nonNull(darenBaseDo)) {
                        darenBaseDo.setCategoryTopDesc(sellUpdateData.getCategoryTopDesc());
                        darenBaseDo.setBrandTopDecs(sellUpdateData.getBrandTopDecs());
                        darenBaseMapper.insertOrUpdate(darenBaseDo);
                    }
                }

            }
        }
        return "解析结束";
    }

    private String getUid(String fristLine, String willRegexData) {
        String[] lineContent = fristLine.split(willRegexData);
        String parameter = lineContent[1].replace(" HTTP/1.1", "");
        String[] parmenterSplit = parameter.split("&");
        String uid = null;
        for (String s : parmenterSplit) {
            String[] specificParmenter = s.split("=");
            if (specificParmenter[0].equals("uid")) {
                uid = specificParmenter[1];
                return uid;
            }
        }
        return uid;
    }


    private String getOriginUid(String fristLine, String willRegexData) {
        String[] lineContent = fristLine.split(willRegexData);
        String parameter = lineContent[1].replace(" HTTP/1.1", "");
        String[] parmenterSplit = parameter.split("&");
        String uid = null;
        for (String s : parmenterSplit) {
            String[] specificParmenter = s.split("=");
            if (specificParmenter[0].equals("origin_uid")) {
                uid = specificParmenter[1];
                return uid;
            }
        }
        return uid;
    }

    public String parseData (String filePath) throws IOException {
        Path path = Paths.get(filePath);
        // 响应文件的内容
        List<String> contentList = Files.readAllLines(path);
        return contentList.get(contentList.size() - 1);
    }


    /**
     * 解压压缩包
     */
    private static Set<String> parseSaz(String tarPaths) {
        Set<String> fileSet = null;
        File tarFolder = new File(tarPaths);
        // 文件夹
        File[] files = tarFolder.listFiles();
        if (Objects.nonNull(files)) {
            for (File file : files) {
                // 文件都重命名一下，后缀改成zip
                String fileName = file.getName();

                String[] split = fileName.split("\\.");
                File newFile = new File(tarPaths + "/" + split[0] + "." + "zip");
                boolean renameFile = file.renameTo(newFile);
//                if (renameFile) {
//                    logger.info("重命名---------{}成功", split[0]);
//                } else {
//                    logger.info("重命名---------{}失败", split[0]);
//                }
            }
            // 获取已经生成的文件进行解压
            File[] renameFiles = tarFolder.listFiles();
            fileSet = new LinkedHashSet<>();
            for (File renameFile : renameFiles) {
//                String[] renameFileName = renameFile.getName().split("\\.");
                try {
//                    logger.info(renameFileName[0]);
                    tarPaths = tarPaths.replace("saz", "unzip");
                    String s = UUID.randomUUID().toString();
                    UnzipUtils.unzip(renameFile.getPath(), tarPaths + "/" + s);
                    fileSet.add(tarPaths + "/" + s + "/" + "raw");
                } catch (Exception e) {
                    logger.error("解压出错了----------{}", e);
                    e.printStackTrace();
                }
            }
        }
        logger.info(JSON.toJSONString(fileSet));
        // 获取解析之后的文件路径
        return fileSet;
    }

}
